﻿using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;

namespace Gmantis.Controls.DataGrid
{
    public class DataGridTextColumn : DataGridBoundColumn
    {
        // Methods
        public DataGridTextColumn()
        {
            this.Initialize();
        }

        public DataGridTextColumn(PropertyInfo property)
            : base(property)
        {
            this.Initialize();
            if (property.PropertyType.GetNonNullableType() == typeof(char))
            {
                this.MaxLength = 1;
            }
        }

        public override bool BeginEdit(FrameworkElement editingElement, RoutedEventArgs routedEventArgs)
        {
            TextBoxBase base2 = editingElement as TextBoxBase;
            if (routedEventArgs is System.Windows.Input.KeyEventArgs)
            {
                string keyString = KeyboardUtil.GetKeyString((System.Windows.Input.KeyEventArgs)routedEventArgs);
                if (keyString != null)
                {
                    base2.SetValue(TextBoxBase.GTextProperty, keyString);
                    base2.Select(1, 0);
                    return true;
                }
                return false;
            }
            if (routedEventArgs is TextCompositionEventArgs)
            {
                base2.Text = (routedEventArgs as TextCompositionEventArgs).Text;
                base2.Select(base2.Text.Length, 0);
            }
            return true;
        }

        public override void CancelCellEdit(FrameworkElement editingElement, object uneditedValue)
        {
            (editingElement as TextBoxBase).Text = (string)uneditedValue;
        }

        public override void EndEdit(FrameworkElement editingElement)
        {
            TextBoxBase base2 = editingElement as TextBoxBase;
            if (base2 != null)
            {
                try
                {
                    BindingExpression bindingExpression = base2.GetBindingExpression(TextBoxBase.GTextProperty);
                    if (bindingExpression != null)
                    {
                        bindingExpression.UpdateSource();
                    }
                }
                catch
                {
                }
            }
        }

        public override FrameworkElement GetCellEditingContent(DataGridRow row)
        {
            TextBoxBase base2 = new TextBoxBase
            {
                TextWrapping = base.TextWrapping,
                Padding = new Thickness(3.0),
                CornerRadius = new CornerRadius(0.0),
                MaxLength = this.MaxLength
            };
            if (base.Binding != null)
            {
                Binding binding = base.CopyBinding(base.Binding, null);
                base2.SetBinding(TextBoxBase.GTextProperty, binding);
            }
            base2.TextAlignment = (base.HorizontalAlignment == HorizontalAlignment.Left) ? TextAlignment.Left : ((base.HorizontalAlignment == HorizontalAlignment.Right) ? TextAlignment.Right : TextAlignment.Center);
            base2.VerticalContentAlignment = base.VerticalAlignment;
            if (base.CellEditingContentStyle != null)
            {
                base2.Style = base.CellEditingContentStyle;
            }
            return base2;
        }

        public override IDataGridFilter GetFilter()
        {
            DataGridFilter filter = new DataGridFilter();
            DataGridTextFilter target = new DataGridTextFilter();
            target.SetBinding<DataGrid>(Control.BackgroundProperty, base.DataGrid, dg => dg.HeaderBackground);
            target.SetBinding<DataGrid>(Control.ForegroundProperty, base.DataGrid, dg => dg.HeaderForeground);
            target.SetBinding<DataGrid>(Control.BorderBrushProperty, base.DataGrid, dg => dg.BorderBrush);
            target.SetBinding<DataGrid>(DataGridTextFilter.MouseOverBrushProperty, base.DataGrid, dg => dg.MouseOverBrush);
            target.SetBinding<DataGrid>(DataGridTextFilter.PressedBrushProperty, base.DataGrid, dg => dg.PressedBrush);
            target.SetBinding<DataGrid>(DataGridTextFilter.SelectedBackgroundProperty, base.DataGrid, dg => dg.SelectedBackground);
            target.SetBinding<DataGrid>(DataGridTextFilter.FocusBrushProperty, base.DataGrid, dg => dg.SelectedBackground);
            target.SetBinding<DataGrid>(DataGridTextFilter.InputBackgroundProperty, base.DataGrid, dg => dg.RowBackground);
            target.SetBinding<DataGrid>(DataGridTextFilter.InputForegroundProperty, base.DataGrid, dg => dg.RowForeground);
            filter.InnerControl = target;
            return filter;
        }

        private void Initialize()
        {
        }

        public override object PrepareCellForEdit(FrameworkElement editingElement)
        {
            return (editingElement as TextBoxBase).Text;
        }

        // Properties
        public override bool IsEditable
        {
            get
            {
                return (base.Binding != null);
            }
        }

        public int MaxLength { get; set; }
    }
}
